gr = require "graphs"

cities = gr.make_graph(0) 
gr.start_naming(cities)


function node_of_city(name)
   local i = gr.node_of_name(cities,name) 
   if i
   then
      return i 
   else 
      gr.extend_graph(cities,1)
      gr.give_node_name(cities,cities.length,name) 
      return gr.node_of_name(cities,name)
      -- node_of_city(name)
   end 
end

function city_of_node(n) 
   return gr.name_of_node(cities,n)
end

function Edge(t) 
   local source = node_of_city(t.src) 
   local dest = node_of_city(t.dst) 

   table.insert(cities[source],{src = source, dst = dest}) 
   table.insert(cities[dest],{src = dest, dst = source}) 
end

function print_edges()
   for i,es in ipairs(cities) do 
      local city_name = gr.name_of_node(cities,i)
      if city_name 
      then 
	 io.write(string.format("\nFrom    %s  	to:   ", city_name))
      else 
	 io.write(string.format("\nNameless city %d  ", i))
      end
      for _,e in pairs(es) do 
	 io.write(string.format(" %s, ", gr.name_of_node(cities,e.dst)))
      end
   end
   io.write("\n\n")
   io.flush()
end

function print_edges_anonymously () 
   for i,es in ipairs(cities) do 
      io.write(string.format("\nFrom    %d  	to:   ", i))
      for _,e in pairs(es) do 
	 io.write(string.format(" %d, ", e.dst))
      end
   end
   io.write("\n\n")
   io.flush()
end   
      

dofile("city_graph.txt")

search = require "hw6_bfs_dfs"

function bfs_results(city_name) 
   search.print_bfs_results(
      cities,
      search.bfs(cities,gr.node_of_name(cities,city_name)))
end 

function dfs_results() 
   local r = search.dfs_stack(cities)
   io.write("City		Disc.		Fin.		Pred.\n\n")
   for i,t in pairs(r) do 
      io.write(string.format("\n %s		%d		%d	%s",
			     gr.name_of_node(cities,i),  
			     t.disc,
			     t.fin, 
			     gr.name_of_node(cities,t.pi) or "none"))
   end 
   io.write("\n"); io.flush()
   --   search.print_dfs_results(cities,r)
end 

--[[
for k, v in ipairs( cities ) do
	print( k )
	for k2, v2 in pairs( v ) do
		print( "", k2, v2 )
		for k3, v3 in pairs( v2 ) do
			print ("","", k3, v3 )
		end
	end
end
]]

bfs_results("Atlanta") 